home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 4
/
Apprentice-Release4.iso
/
Languages
/
Open Prolog 1.0.3d23
/
Contributions from Others
/
Henk Schotel
/
Knights Tour
next >
Wrap
Text File
|
1993-10-06
|
4KB
|
151 lines
% SIMPLE-KNIGHTS-TOUR: Knight jumping on a 3x3 board.
% From Luger & Stubblefield - AI and the Design of Expert Systems (1989).
% Modified and Open Prolog graphics added by
%
% Henk Schotel
% Nymegen University - Philosophy
% Postbox 9108
% NL-6500 HK Nijmegen
% The Netherlands
%
% e-mail: HSCHOTEL@KUNRC1.URC.KUN.NL
%
% SIMPLE-KNIGHTS-TOUR
% This implements the simplified knight's tour problem from
% section 6.1.2. It uses lists to keep track of previously visited
% states.
% to run it, give PROLOG a path/3 goal with the start and the goal
% and a 3rd argument for the solution. For example, to test for a path
% from square 2 to square 4, enter the goal: path(2, 4, R).
% ?-path(2, 4, R). % R will contain the path
%These predicates enumerate the moves of the knight's tour.
move(1,6).
move(1,8).
move(2,7).
move(2,9).
move(3,4).
move(3,8).
move(4,3).
move(4,9).
move(6,7).
move(6,1).
move(7,6).
move(7,2).
move(8,3).
move(8,1).
move(9,4).
move(9,2).
path(P,Q,R):-
init_pen_size,
draw_board(P,Q),
path(P,Q,[P],R),
draw_path(R),
increment_pen_size. % For the next solution
% to run path/4 give PROLOG a path goal with the start, goal [and been list
% instantiated to the desired values.] For example, to test for a path
% from square 2 to square 4, enter the goal: path(2, 4, [2], R ).
% the recursive path predicate controls the search
path(Z,Z,L,R) :-
reverse(L,R).
path(X,Y,L,R) :-
move(X,Z),
not(member(Z,L)),
path(Z,Y,[Z|L],R).
% The member predicate tests for membership of an element in a
% list.
member(X,[X|T]).
member(X,[Y|T]) :- member(X,T).
reverse(L,R):-
reverse(L,[],R).
reverse([],R,R).
reverse([H|T],S,R):-
reverse(T,[H|S],R).
%-------------------------------------------------------------------------
% Graphics:
%-------------------------------------------------------------------------
draw_path([_]).
draw_path([P,Q|Rest]):-
draw_jump(P,Q),
draw_path([Q|Rest]).
draw_jump(P,Q):-
xy(P,(Xp,Yp)),
xy(Q,(Xq,Yq)),
draw(3,line(Xp,Yp,Xq,Yq),_).
% 1 2 3
% 4 5 6
% 7 8 9
xy(1,(50,50)).
xy(2,(150,50)).
xy(3,(250,50)).
xy(4,(50,150)).
xy(5,(150,150)).
xy(6,(250,150)).
xy(7,(50,250)).
xy(8,(150,250)).
xy(9,(250,250)).
draw_board(P,Q) :-
draw(2,_,_),
draw(1,path(50,20,368,338),_), % T,L,Hlength,VLength
% (18 extra for the scroll bars!)
draw(6,line(3,3),_), % pensize 3
draw(4,rect(0,0,303,303),_), % 1 point of white to try and understand
draw(3,line(4,100,296,100),_),
draw(3,line(4,200,296,200),_),
draw(3,line(100,4,100,296),_),
draw(3,line(200,4,200,296),_),
draw_square(P),
draw_dot(Q),
draw(6,line(1,1),_).
draw_dot(P):-
xy(P,(X,Y)),
L is X-4,
T is Y-4,
R is X+6,
B is Y+6,
draw(6,line(2,2),_), % pensize 2
draw(12,oval(L,T,R,B),_),
path_pen_size(S),
draw(6,line(S,S),_). % pensize Old
draw_square(P):-
xy(P,(X,Y)),
L is X-4,
T is Y-4,
R is X+6,
B is Y+6,
draw(6,line(2,2),_), % pensize 2
draw(4,rect(L,T,R,B),_),
path_pen_size(S),
draw(6,line(S,S),_). % pensize Old
init_pen_size :-
abolish(path_pen_size,1),
assert(path_pen_size(1)).
increment_pen_size :-
retract(path_pen_size(PS)),
NewPS is PS + 1,
draw(6,line(NewPS,NewPS),_),
assert(path_pen_size(NewPS)).
%-------------------------------------------------------------------------